#include <iostream>
#include <unistd.h>
#include <signal.h>
#include "Sunnet.h"
using namespace std;

void test() {
	auto pingType1 = make_shared<string>("ping1");
	auto pingType2 = make_shared<string>("ping2");
	auto pingType3 = make_shared<string>("pong");
	uint32_t ping1 = Sunnet::instance->NewService(pingType1);
	uint32_t ping2 = Sunnet::instance->NewService(pingType2);
	uint32_t pong = Sunnet::instance->NewService(pingType3);

	auto msg1 = Sunnet::instance->MakeMsg(ping1, new char[3]{'h', 'i', '\0'}, 3);
	auto msg2 = Sunnet::instance->MakeMsg(ping2, new char[6]{'h', 'i', ',', 'n', 'h', '\0'}, 3);

	Sunnet::instance->Send(pong, msg1);
	Sunnet::instance->Send(pong, msg2);
}

void test_socket() {
	int fd = Sunnet::instance->Listen(7771, 1);
	usleep(10000000);
	Sunnet::instance->CloseConn(fd);
}

void test_echo() {
	auto gatewayStr = make_shared<string>("gateway");
	uint32_t gateway = Sunnet::instance->NewService(gatewayStr);
}

void signalHandler(int signal) {
	cout << "收到 SIGINT 信号，开始资源回收..." << endl;
	exit(0);
}

int main() {
	// const auto util = new Util();

	signal(SIGINT, signalHandler);

	new Sunnet();
	Sunnet::instance->Start();

	// test();
	// test_socket();
	// test_echo();
	auto mainStr = make_shared<string>("main");
	uint32_t main = Sunnet::instance->NewService(mainStr);

	Sunnet::instance->Wait();
	return 0;
}